//
//  LFHomeViewController.m
//  QingDaoCommunity
//
//  Created by 李学林 on 15/11/21.
//  Copyright © 2015年 teteTechnology. All rights reserved.
//

#import "LFHomeViewController.h"
#import "LFLoginViewController.h"
#import <CoreLocation/CoreLocation.h>
#import "LFLocation.h"
#import "LFHomeModel.h"
#import "LFAFHTTPClient.h"
#import "LFFocus.h"
#import "LFAdImageViewCell.h"
#import "LFGroupTableViewCell.h"
#import "LFFamousTableViewCell.h"
#import "LFGuessTableViewCell.h"

@interface LFHomeViewController ()<CLLocationManagerDelegate,UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) LFHomeModel *homeModel;

@property (nonatomic,strong) CLLocationManager *manager;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation LFHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //调用网络加载的视图
    [self showLoadView];

    [self performSelector:@selector(dissmissLoadView) withObject:nil afterDelay:2.0];
    
    [self showToastWithMessage:@"hello world"];

    self.leftBtn.hidden = YES;
    
    // 加载数据(发送请求加载数据)
    
    [self loadData];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    // 定位
    //    [self getLocation];
    [LFLocation locationSuccess:^(CLLocationCoordinate2D coordinate2D) {
        //成功
    DSLog(@"hello1");
    
    DSLog(@"%f",coordinate2D.latitude);
    
    // 在这里定位成功之后应该把经纬度作为参数传递数据给加载数据方法,因为这里是北京不是青岛,所以在加载当地数据的时候直接给了一个固定的坐标[self loadData:()经度,djkdjfkd:()weidu];
    } orFail:^(NSError *error) {
       //失败
        NSLog(@"%@",error);
    }];
   // 注册
   //   [self.tableView registerClass:[LFAdImageViewCell class] forCellReuseIdentifier:@"FocusCell"];
    // 给tabView设置headerView
    _header = [[NSBundle mainBundle]loadNibNamed:@"LFAdImageViewCell" owner:nil options:nil].lastObject;
    
    _header.frame = CGRectMake(0, 0,kScreemWidth, (425-88)/2.0);
    // 赋值
    self.tableView.tableHeaderView = _header;
    
 }

#pragma mark----------UITableViewDataSource---------

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 3;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (section == 0 || section == 1) {
        
        return 1;
    }else {
        return self.homeModel.guess.list.count;
    }

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [[UITableViewCell alloc]init];
    
    if (indexPath.section == 0) {
        static NSString *identifer = @"LFGroupTableViewCell";
        LFGroupTableViewCell *cell =[[[UINib nibWithNibName:identifer bundle:nil]instantiateWithOwner:self options:nil]objectAtIndex:0];
        
        cell.homeModel = self.homeModel;
        return cell;
    }

    if(indexPath.section == 1){
        LFFamousTableViewCell *cell = [[[UINib nibWithNibName:@"LFFamousTableViewCell" bundle:nil]instantiateWithOwner:self options:nil]objectAtIndex:0];
        
        cell.famousList = self.homeModel.famous;
        
        return cell;
    }
    
    if (indexPath.section == 2) {
        
        LFGuessTableViewCell *cell = [[[UINib nibWithNibName:@"LFGuessTableViewCell" bundle:nil]instantiateWithOwner:self options:nil]objectAtIndex:0];
        
        cell.guess = self.homeModel.guess.list[indexPath.row];
        
        return cell;
        
    }
        return cell;
}

#pragma mark -------------UITableViewDelegate--------------
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
 
    if (indexPath.section == 0) {
        
        return 200;
        
    }
    if (indexPath.section ==1) {
        return 200;
    }
    
    if (indexPath.section == 2) {
        
        return 130;
    }
    
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return 10;

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    if (section == 1 || section == 2) {
        
        return 40;
    }

    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 40)];
    
    view.backgroundColor = [UIColor whiteColor];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, 40)];
    
    [view addSubview:label];
    
    if (section == 1) {
    
    label.text = @"名店推荐";

        
    }else if(section == 2){
    
    label.text = @"猜你喜欢";
    
    }
    
    return view;
}



#pragma mark ------------------------登录方法-------------------------------
- (void)loginMethod{

    LFLoginViewController *login = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"LFLoginViewController"];
    

    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [app.tab showOrHiddenTabbarView:YES];
    
    
    [self.navigationController pushViewController:login animated:YES];

}

#pragma -----------------------------加载数据--------------------------------
- (void)loadData{

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    NSMutableDictionary *para = [NSMutableDictionary dictionary];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    //拼接参数
    //经度
    [para setValue:@"120.38" forKey:@"lon"];
    //纬度
    [para setValue:@"36.06" forKey:@"lat"];
    
    //时间戳
    NSString *str = [LFFunction getTimeThuo];
    
    [para setValue:str forKey:@"time"];
    
    // 5bV9JhaMn2GJ5MZe
    /*加密签名，由所有参数值与约定的平台标识符连接，进行md5加密生成，参数连接顺序和上面提供的顺序相同。
     加密规则:sign=md5(app_id + lon+lat + time + key);
     */
    NSArray *tempArray = @[APP_ID,para[@"lon"],para
                          [@"lat"],para[@"time"],APP_INDENTIFIER];
    
    [para setValue:[LFFunction MD5WithArray:tempArray] forKey:@"sign"];
    
    // 注意一定要打印参数-----这里是一个坑
    DSLog(@"%@",para); 
    
    
    [LFAFHTTPClient getHomeWithParas:para WithRequestSuccess:^(LFHomeModel *model) {
    //这里得到的就是解析好的模型了
        NSLog(@"%@",model);

        self.header.homeModel = model;
        
        self.homeModel = model;
        
        [self.tableView reloadData];
   
    } WithFailled:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    
}


@end
